
if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false

end

if ( CLIENT ) then

	SWEP.DrawAmmo			= true
	SWEP.DrawCrosshair		= false
	SWEP.ViewModelFOV		= 70
	SWEP.ViewModelFlip		= true
	SWEP.CSMuzzleFlashes	= true
	SWEP.PitchMod = 1
	SWEP.RollMod = 1
	// This is the font that's used to draw the death icons
	surface.CreateFont( "csd", ScreenScale( 30 ), 500, true, true, "CSKillIcons" )
	surface.CreateFont( "csd", ScreenScale( 60 ), 500, true, true, "CSSelectIcons" )

end

SWEP.Author			= "LEETNOOB"
SWEP.Contact		= ""
SWEP.Purpose		= ""
SWEP.Instructions	= "PRIMARY ATTACK key to fire\nSECONDARY ATTACK key to toggle ironsights\nUSE key + SECONDARY ATTACK key - attach suppressor\nUSE key + RELOAD key - switch between semi-auto/auto firemodes (SMGs/ARs)"

// Note: This is how it should have worked. The base weapon would set the category
// then all of the children would have inherited that.
// But a lot of SWEPS have based themselves on this base (probably not on purpose)
// So the category name is now defined in all of the child SWEPS.
//SWEP.Category			= "Counter-Strike"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= false

SWEP.Primary.Sound			= Sound( "Weapon_AK47.Single" )
SWEP.Primary.Recoil			= 1.5
SWEP.Primary.Damage			= 40
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.02
SWEP.Primary.Delay			= 0.15

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"
SWEP.Base = "cstm_base_pistol"

function SWEP:SecondaryAttack()
	if self.Weapon:GetDTInt(3) == 2 or not self.Owner:OnGround() or self.Weapon.IsReloading == true then
		return
	end

	if self.Weapon.ShouldBolt == true then
		return
	end

	if self.Owner:KeyDown(IN_USE) and self.Weapon:GetDTInt(3) != 1 then
		if self.Weapon.CantSilence == true then
			return
		end
	
		if self.Weapon.IsSilenced == false then
			
			if SERVER then
				self.Weapon:SetDTInt(3, 6)
				
				timer.Simple(0.5, function()
					if self.Weapon == nil then
						return
					end
					
					self.Owner:EmitSound("weapons/usp/usp_silencer_on.wav", 50, math.random(99, 101))
					self.Owner:DoAnimationEvent(ACT_ITEM_GIVE)
				end)
				
				timer.Simple(2, function()
					if self.Weapon == nil then
						return
					end
					
					self.Weapon:NWAlpha(255)
					self.Weapon.IsSilenced = true
					self.Weapon:SetDTInt(3, 7)
				end)
			end
			
		else
			if SERVER then
				self.Weapon:SetDTInt(3, 6)
				
				timer.Simple(0.5, function()
					if self.Weapon == nil then
						return
					end
					
					self.Owner:EmitSound("weapons/usp/usp_silencer_off.wav", 50, math.random(99, 101))
					self.Owner:DoAnimationEvent(ACT_ITEM_GIVE)
				end)
				
				timer.Simple(1.8, function()
					if self.Weapon == nil then
						return
					end
					
					self.Weapon:NWAlpha(1)
					self.Weapon.IsSilenced = false
					self.Weapon:SetDTInt(3, 7)
				end)
			end
			
		end
		self.Weapon:SetNextPrimaryFire(CurTime() + 2.35)
		self.Weapon:SetNextSecondaryFire(CurTime() + 2.35)
		self.Weapon.ReloadDelay = CurTime() + 2.35
		return
	end
	
	if (self.Weapon:GetDTInt(3) == 0 or self.Weapon:GetDTInt(3) == 3 or self.Weapon:GetDTInt(3) == 7) then
		self.Weapon:SetDTInt(3, 1)
		if SERVER then
			timer.Simple(0.15, function()
				if self.Weapon == nil or self.Weapon:GetDTInt(3) != 1 then
					return
				end
				self.Owner:DrawViewModel(false)
			end)
			self.Owner:SetFOV(self.Owner:GetFOV() - (self.Weapon.ZoomAmount or 70), 0.3)
		end
	elseif self.Weapon:GetDTInt(3) == 1  then
		self.Weapon:SetDTInt(3, 0)
		if SERVER then
			self.Owner:SetFOV(0, 0.3)
			self.Owner:DrawViewModel(true)
		end
	end
	
	self.Weapon:SetNextPrimaryFire(CurTime() + 0.2)
	self.Weapon:SetNextSecondaryFire(CurTime() + 0.3)
	
end


if CLIENT then

	local gap = 5
	local ScopeTexture = surface.GetTextureID("gcellmats/scope_rifle2")
	local Bullet = surface.GetTextureID("VGUI/bullet")
	local CurAlpha_Weapon = 255

	function SWEP:DrawHUD()
		local Width = ScrW()
		local Height = ScrH()
		local x2 = (Width - 1024) / 2
		local y2 = (Height - 1024) / 2
		local x3 = Width - x2
		local y3 = Height - y2
		local FT = FrameTime()
		if self.Weapon:GetDTInt(3) == 1 then
			if UseGoggle == false then
				TimeToUseGoggle = CurTime() + 0.15
				UseGoggle = true
			end
			
			if CurTime() > TimeToUseGoggle then
				surface.SetDrawColor(255, 255, 255, 255)
				surface.SetTexture(ScopeTexture)
				surface.DrawTexturedRectUV(x2, y2, 1024, 1024, 1024, 1024)
				
				surface.SetDrawColor(0, 0, 0, 255)
				surface.DrawRect(0, 0, x2, Height)
				surface.DrawRect(x3, 0, x2, Height)
				surface.DrawRect(0, 0, Width, y2)
				surface.DrawRect(0, y3, Width, y2)
			end
		else
			UseGoggle = false
		end
		
		if self.Weapon.Bullets then
		
			local Text
		
			if self.Weapon.Bullets == 1 then
				Text = "Semi-auto"
			elseif self.Weapon.Bullets == 3 then
				Text = "Burst"
			elseif self.Weapon.Bullets == 5 then
				Text = "Full-auto"
			end
			
			draw.RoundedBox(6, Width * 0.5 - 60, Height * 0.9, 120, 30, Color(0, 0, 0, 220))
			draw.SimpleText(Text, "DefaultSmallDropShadow", Width * 0.5, Height * 0.9 + 10, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)	

			surface.SetDrawColor(255, 210, 0, 255)			
		
			if self.Weapon.Bullets == 1 then
				surface.SetTexture(Bullet)
				surface.DrawTexturedRectRotated(Width * 0.5, Height * 0.9 + 20, 16, 16, 180)
			elseif self.Weapon.Bullets == 3 then
			
				for i = 1, 3 do
					surface.SetDrawColor(255, 210, 0, 255)
					surface.SetTexture(Bullet)
					surface.DrawTexturedRectRotated(Width * 0.5 + 20 * i - 40, Height * 0.9 + 20, 16, 16, 180)
				end
				
			else
			
				for i = 1, 5 do
					surface.SetDrawColor(255, 210, 0, 255)
					surface.SetTexture(Bullet)
					surface.DrawTexturedRectRotated(Width * 0.5 + 20 * i - 60, Height * 0.9 + 20, 16, 16, 180)
				end
				
			end
		end	
		
		if ( self.Owner == LocalPlayer() && self.Owner:ShouldDrawLocalPlayer() ) then

			local tr = util.GetPlayerTrace( self.Owner )
			tr.mask = ( CONTENTS_SOLID|CONTENTS_MOVEABLE|CONTENTS_MONSTER|CONTENTS_WINDOW|CONTENTS_DEBRIS|CONTENTS_GRATE|CONTENTS_AUX )
			local trace = util.TraceLine( tr )
			
			local coords = trace.HitPos:ToScreen()
			x, y = coords.x, coords.y

		else
			x, y = Width / 2.0, Height / 2.0
		end
		
		local scale = 10 * self.Weapon:GetDTFloat(0) //self.Primary.Cone
		
		if self.Weapon:GetDTInt(3) == 2 or self.Weapon:GetDTInt(3) == 4 then
			CurAlpha_Weapon = math.Approach(CurAlpha_Weapon, 0, FT / 0.0017)
		elseif (self.Weapon:GetDTInt(3) == 1 and self.Weapon.NoProperIronsights != true) or UseGoggle == true or (self.Weapon:GetDTInt(3) == 1 and self.Weapon.HasInstalledScope == true) then
			CurAlpha_Weapon = math.Approach(CurAlpha_Weapon, 0, FT / 0.001)
		elseif self.Weapon:GetDTInt(3) == 0 or self.Weapon:GetDTInt(3) == 3 then
			CurAlpha_Weapon = math.Approach(CurAlpha_Weapon, 255, FT / 0.001)
		end
		
		gap = Lerp(0.25, gap, scale * 50)
		
		if GetConVarNumber("cstm_dynamiccrosshair") > 0 then
			gap2 = gap
		else
			gap2 = 0
		end
		
		if GetConVarNumber("cstm_leftcross") > 0 then
			surface.SetDrawColor(GetConVarNumber("cstm_out_r"), GetConVarNumber("cstm_out_g"), GetConVarNumber("cstm_out_b"), CurAlpha_Weapon * (GetConVarNumber("cstm_out_a") / 255))
			surface.DrawRect(x - gap - GetConVarNumber("cstm_crosslength") - (gap2 * 0.4), y - 1, GetConVarNumber("cstm_crosslength") + 2 + (gap2 * 0.4), 3)
			surface.SetDrawColor(GetConVarNumber("cstm_in_r"), GetConVarNumber("cstm_in_g"), GetConVarNumber("cstm_in_b"), CurAlpha_Weapon * (GetConVarNumber("cstm_in_a") / 255))
			surface.DrawRect(x - gap - (GetConVarNumber("cstm_crosslength") - 1) - (gap2 * 0.4), y, GetConVarNumber("cstm_crosslength") + (gap2 * 0.4), 1)
		end
		
		if GetConVarNumber("cstm_rightcross") > 0 then
			surface.SetDrawColor(GetConVarNumber("cstm_out_r"), GetConVarNumber("cstm_out_g"), GetConVarNumber("cstm_out_b"), CurAlpha_Weapon * (GetConVarNumber("cstm_out_a") / 255))
			surface.DrawRect(x + gap - 1, y - 1, GetConVarNumber("cstm_crosslength") + 2 + (gap2 * 0.4), 3)
			surface.SetDrawColor(GetConVarNumber("cstm_in_r"), GetConVarNumber("cstm_in_g"), GetConVarNumber("cstm_in_b"), CurAlpha_Weapon * (GetConVarNumber("cstm_in_a") / 255))
			surface.DrawRect(x + gap, y, GetConVarNumber("cstm_crosslength") + (gap2 * 0.4), 1)
		end
		
		if GetConVarNumber("cstm_lowercross") > 0 then
			surface.SetDrawColor(GetConVarNumber("cstm_out_r"), GetConVarNumber("cstm_out_g"), GetConVarNumber("cstm_out_b"), CurAlpha_Weapon * (GetConVarNumber("cstm_out_a") / 255))
			surface.DrawRect(x - 1, y + gap - 1, 3, GetConVarNumber("cstm_crosslength") + 2 + (gap2 * 0.4))
			surface.SetDrawColor(GetConVarNumber("cstm_in_r"), GetConVarNumber("cstm_in_g"), GetConVarNumber("cstm_in_b"), CurAlpha_Weapon * (GetConVarNumber("cstm_in_a") / 255))
			surface.DrawRect(x , y + gap, 1, GetConVarNumber("cstm_crosslength") + (gap2 * 0.4))
		end
		
		if GetConVarNumber("cstm_uppercross") > 0 then
			surface.SetDrawColor(GetConVarNumber("cstm_out_r"), GetConVarNumber("cstm_out_g"), GetConVarNumber("cstm_out_b"), CurAlpha_Weapon * (GetConVarNumber("cstm_out_a") / 255))
			surface.DrawRect(x - 1, y - gap - GetConVarNumber("cstm_crosslength") - (gap2 * 0.4), 3, GetConVarNumber("cstm_crosslength") + 2 + (gap2 * 0.4))
			surface.SetDrawColor(GetConVarNumber("cstm_in_r"), GetConVarNumber("cstm_in_g"), GetConVarNumber("cstm_in_b"), CurAlpha_Weapon * (GetConVarNumber("cstm_in_a") / 255))
			surface.DrawRect(x, y - gap - (GetConVarNumber("cstm_crosslength") - 1) - (gap2 * 0.4), 1, GetConVarNumber("cstm_crosslength") + (gap2 * 0.4))
		end
	end
end